package com.xr.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xr.pojo.Goods;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class BookDaoTestCase {

    @Autowired
    private BookDao bookDao;

    @Test
    void testGetById(){
        System.out.println(bookDao.selectById(1));
    }
    @Test
    void testSave(){
        Goods goods = new Goods();
        goods.setInfo("测试数据123");
        goods.setName("测试数据123");
        goods.setPrice(11);
        bookDao.insert(goods);
    }

    @Test
    void testUpdate(){
        Goods goods = new Goods();
        goods.setId(11);
        goods.setInfo("测试数据abcdefg");
        goods.setName("测试数据123");
        goods.setPrice(11);
        bookDao.updateById(goods);
    }
    @Test
    void testDelete(){
        bookDao.deleteById(11);
    }
    @Test
    void testGetAll(){
        List<Goods> goods = bookDao.selectList(null);
        System.out.println(goods);
    }

    @Test
    void testGetPage(){
        IPage page = new Page(2,5);
        bookDao.selectPage(page, null);
        System.out.println(page.getCurrent()); //当前页码值
        System.out.println(page.getSize()); //每页显示数
        System.out.println(page.getTotal()); //数据总量
        System.out.println(page.getPages()); //总页数
        System.out.println(page.getRecords()); //详细数据
    }

    @Test
    void testGetBy(){
        //1.创建封装查询条件的对象
        QueryWrapper<Goods> qw = new QueryWrapper<>();
        //2.设置条件
        qw.like("name","小廖");
        //3.查询
        List<Goods> goods = bookDao.selectList(qw);
        System.out.println(goods);
    }

    @Test
    void testGetBy2(){
        String name = "小";
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<Goods>();
        lqw.like(Goods::getName,name);
        bookDao.selectList(lqw);
    }

    @Test
    void testGetBy3(){
        String name = "小";
        LambdaQueryWrapper<Goods> lqw = new LambdaQueryWrapper<Goods>();
        //if(name != null) lqw.like(Book::getName,name); //方式一：JAVA代码控制
        lqw.like(name != null,Goods::getName,name); //方式二：API接口提供控制开关
        bookDao.selectList(lqw);
    }





}
